package Member_Message;

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.math.BigDecimal;
import java.util.ArrayList;

public class Member_Table extends JFrame {
    private DefaultTableModel tableModel;
    private JTable table;
    private ArrayList<JTextField> TextFields = new ArrayList<>();
    private int selectedRow;
    private String[] columnNames={"name","kind","address","tel","email","grade","direction","tutor","office","salary","duty","when_emploied","worktime","level"};

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Member_Table member_table = new Member_Table();
        member_table.setVisible(true);
    }

    private Member_Table() throws IOException, ClassNotFoundException {
        super();
        setTitle("Member message talbe");
        setBounds(-10,20,2000,1000);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        final JScrollPane jScrollPane = new JScrollPane(table);
        getContentPane().add(jScrollPane,BorderLayout.CENTER);

        String[][] tablevalue;
        try(ObjectInputStream objectInputStream =
                    new ObjectInputStream(new FileInputStream("persons.dat"))) {
            Person[] persons = (Person[]) (objectInputStream.readObject());
            tablevalue = getinformation(persons);
        }

        tableModel = new DefaultTableModel(tablevalue, columnNames);
        table = new JTable(tableModel);
        table.setRowSorter(new TableRowSorter<>(tableModel));

        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        for(int i = 0; i < 14; i++){
            if (i == 0)
                TextFields.add(new JTextField(columnNames[i]+table.getRowCount(),5));
            else
                TextFields.add(new JTextField("无",5));
        }

        table.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent event){
                selectedRow = table.getSelectedRow();
                ArrayList<Object> objects = new ArrayList<>();
                for(int i = 0; i < 14; i++){
                    Object o =tableModel.getValueAt(selectedRow,i);
                    objects.add(o);
                }
                for(int i = 0; i < 14; i++){
                    TextFields.get(i).setText(objects.get(i).toString());
                }
            }
        });
        jScrollPane.setViewportView(table);
        final JPanel jPanel = new JPanel();
        getContentPane().add(jPanel,BorderLayout.SOUTH);

        for(int i = 0; i < 14; i++){
            jPanel.add(new Label(columnNames[i]));
            jPanel.add(TextFields.get(i));
        }

        final JButton addButton = new JButton("添加");
        addButton.addActionListener(e -> {
            String[] rowValues = new String[14];
            for(int i = 0; i < 14; i++){
                rowValues[i] = TextFields.get(i).getText();
            }
            tableModel.addRow(rowValues);
            int rowCount = table.getRowCount()+1;
            for (int i = 0; i < 14; i++){
                TextFields.get(i).setText(columnNames[i] + rowCount);
            }
        });
        jPanel.add(addButton);

        final JButton updButton = new JButton("修改");
        updButton.addActionListener(e -> {
            selectedRow = table.getSelectedRow();
            if(selectedRow != -1){
                for(int i = 0; i < 14; i++){
                    tableModel.setValueAt(TextFields.get(i).getText(),selectedRow,i);
                }
            }
        });
        jPanel.add(updButton);

        final JButton delButton = new JButton("删除");
        delButton.addActionListener(e ->  {
            selectedRow = table.getSelectedRow();
            if (selectedRow != -1){
                tableModel.removeRow(selectedRow);
            }
        });
        jPanel.add(delButton);

        final JButton serButton = new JButton("查询");
        serButton.addActionListener(e -> {
            String name = TextFields.get(0).getText();
            for (int i = 0; i <= tableModel.getRowCount(); i++){
                if (i == tableModel.getRowCount()){
                    System.out.println("No such member");
                    new FoundDialog(Member_Table.this,false).setVisible(true);
                    break;
                }
                if(tableModel.getValueAt(i, 0).toString().equals(name)){
                    selectedRow = i;
                    for(int j = 0; j < 14; j++){
                        if (!tableModel.getValueAt(selectedRow, j).toString().equals("无"))
                            TextFields.get(j).setText(tableModel.getValueAt(selectedRow,j).toString());
                        else
                            TextFields.get(j).setText("无");
                    }
                    new FoundDialog(Member_Table.this,true).setVisible(true);
                    break;
                }
            }
        });
        jPanel.add(serButton);
    }
    private String[][] getinformation(Person[] persons){
        String[][] strings = new String[persons.length][14];
        for (int i = 0; i < persons.length;i++) {
            strings[i][0] = persons[i].name;
            strings[i][2] = persons[i].address;
            strings[i][3] = ((Integer)(persons[i].telephonenumber)).toString();
            strings[i][4] = persons[i].email;
            if (persons[i] instanceof Student){
                Student student = (Student)persons[i];
                strings[i][5] = ((Integer)(student.grade)).toString();
                strings[i][8] = "无";
                strings[i][9] = "无";
                strings[i][10] = "无";
                strings[i][11] = "无";
                strings[i][12] = "无";
                strings[i][13] = "无";
                if(persons[i] instanceof Postgraduate){
                    Postgraduate postgraduate = (Postgraduate) persons[i];
                    strings[i][6] = postgraduate.direction;
                    strings[i][7] = postgraduate.tutor;
                    strings[i][1] = "Postgraduate";
                }
                else {
                    strings[i][6] = "无";
                    strings[i][7] = "无";
                    strings[i][1] = "Student";
                }
            }
            else if (persons[i] instanceof Employee){
                Employee employee = (Employee)persons[i];
                BigDecimal bigDecimal = new BigDecimal(employee.salary);
                strings[i][5] = "无";
                strings[i][6] = "无";
                strings[i][7] = "无";
                strings[i][8] = employee.office;
                strings[i][9] = bigDecimal.toString();
                strings[i][11] = employee.whenemploy.toString();
                if (persons[i] instanceof Faculty) {
                    Faculty faculty = (Faculty)persons[i];
                    BigDecimal bigDecimal2 = new BigDecimal(faculty.worktime);
                    strings[i][1] = "Faculty";
                    strings[i][10] = "无";
                    strings[i][12] = bigDecimal2.toString();
                    strings[i][13] = faculty.level;
                }
                else if (persons[i] instanceof Staff){
                    Staff staff = (Staff)persons[i];
                    strings[i][1] = "Staff";
                    strings[i][10] = staff.duty;
                    strings[i][12] = "无";
                    strings[i][13] = "无";
                }
                else {
                    strings[i][1] = "Employee";
                    strings[i][5] = "无";
                    strings[i][6] = "无";
                    strings[i][7] = "无";
                    strings[i][10] = "无";
                    strings[i][12] = "无";
                    strings[i][13] = "无";
                }
            }
            else {
                strings[i][1] = "Person";
                strings[i][5] = "无";
                strings[i][6] = "无";
                strings[i][7] = "无";
                strings[i][8] = "无";
                strings[i][9] = "无";
                strings[i][10] = "无";
                strings[i][11] = "无";
                strings[i][12] = "无";
                strings[i][13] = "无";
            }
        }
        return strings;
    }
    class FoundDialog extends JDialog{
        private Container container;
        FoundDialog(Member_Table member_table, boolean found){
            super(member_table, "", true);
            container = getContentPane();
            if (found)
                container.add(new JLabel("found member at : " + selectedRow + " row"));
            else
                container.add(new JLabel("No designated members were found"));
            setBounds(1000,500,250,100);
        }
    }
}
